#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

class Solution
{
public:
    int subarraySum(vector<int>& nums, int k)
    {
        unordered_map<int, int> hash;
        int n = nums.size(), sum = 0, ret = 0;
        hash[0] = 1;
        for (auto& e : nums)
        {
            sum += e;
            if (hash.count(sum - k))
                ret += hash[sum - k];
            hash[sum]++;
        }

        return ret;
    }
};

int main()
{
    vector<int> nums = { 1,1,1 };
    cout << Solution().subarraySum(nums, 2);// 2
    return 0;
}